$(function(){
	
	var main=[[ //表头
					 {field: 'username', title: '账号',  sort: true, fixed: 'left'}
			        ,{field: 'role_name', title: '角色',  sort: true}
			        ,{field: 'mobile', title: '手机号'} 
			        ,{field: 'state2', title: '状态'}
			       ,{ title: '操作', toolbar:'#operation1',fixed: 'right'}
			        
			    ]];
			    var fi=1;
			   var data={
			   			page:1,
			   			pagenum:10,
						where_json:JSON.stringify({
							company_id:['in','{$company.company_id}']
						})
					}
	
	{if $Think.get.driver_id}
	Load()
		$.ajax({
			type:'post',
			data:{
				page:1,
				pagenum:10,
				where_json:JSON.stringify({
					driver_id:['in','{$Think.get.driver_id}']
				})
			},
			url:"{:url('adminapi/driver/driverList')}",
			success:function(res){
				Load(1)
				console.log(res)
				var lag=res.data[0].language_enum.split(',')
				console.log(lag)
				for(var li=0;li<lag.length;li++){
					console.log('val')
					$('.language').find(`option`).each(function(){
						console.log()
						if($(this).val()==lag[li]){
							$(this).attr('selected', true);
						}
					})
				}
				$('.language').selected('destroy')
				$('.language').selected('enable')
				
			},
			error:function(){
				Load(1)
				Eee('请求失败')
			}
		})
	{/if}
		
		$('.username').on('click',function(){
			var useridx=layer.open({
				type: 1,
				title:'请选择用户',
				skin: 'layui-layer-rim', //加上边框
				area: ['75%', '75%'], //宽高
				content: `
					<div style="width:100%;height:100%">
						<div class="table_margin_0">
							<table  class="layui-hide" id="user" lay-filter="user"></table>
						</div>
						<div id="page"></div>
						
					</div>`,
				success: function(layero, index) {
					ajaxuser(data,index)
					
				}
			});
		})
	function ajaxuser(data, useridx) {
		console.log(useridx)
		Load()
		$.ajax({
			type: 'post',
			url: '{:url("adminapi/User/userList")}',
			data: data,
			success: function(res) {
				Load(1)
				for(var iii = 0; iii < res.list.length; iii++) {
					res.list[iii].role_name = '';
					for(var ii2 = 0; ii2 < res.list[iii].roles.length; ii2++) {
						if(ii2 == 0) {
							res.list[iii].role_name += res.list[iii].roles[ii2].role_name
						} else {
							res.list[iii].role_name += ("," + res.list[iii].roles[ii2].role_name)
						}
					}
					if(res.list[iii].state == 1) {
						res.list[iii].state2 = '启用'
					} else {
						res.list[iii].state2 = '禁用'
					}
				}
				var table = layui.table,
					form = layui.form,
					laypage = layui.laypage
				table.render({
					elem: '#user',
					cellMinWidth: 80,
					cols: main,
					data: res.list,
					limit: data.pagenum,
					even: true
				});
				if(1) {
					laypage.render({
						elem: 'page',
						count: res.count,
						layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
						curr: data.page,
						jump: function(obj, first) {
							if(!first) {
								page = obj.curr
								data.page = page
								pagenum = obj.limit
								data.pagenum = pagenum
								ajaxuser(data, useridx)
							} else {

							}
						}
					});
					fi = 2
				}
				table.on('tool(user)', function(obj) {
					var data1 = obj.data; //获得当前行数据
					var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
					var tr = obj.tr; //获得当前行 tr 的DOM对象
					if(layEvent === 'choose') {
						$('.user_id').val(data1.user_id)
						$('.username').val(data1.username)
						$('input[name="phone"]').val(data1.mobile)
						layer.close(useridx);
					}
				})
			},
			error: function() {
				Err('请求失败');
				Load(1)
			}
		})
	}	
		
	$("#loading-first").fadeOut(500);
	$('button').on('click',function(){
		 	if($(this).data('type')=='file'){
		 		$(this).parent().find('input[type=file]').click()
		 	}
		});
	$('.keep').on('click',function(){
		{if $Think.get.remote}
		sendForm($(this), 1,'{:url("/admin/custom/driver")}');
		{else/}
			{if $Think.get.driver_id}
			sendForm($(this), 1,window.location.href);
			{else/}
			
			sendForm($(this), 1,'{:url("index")}');
			{/if}
		{/if}
	});
	$('.car_rental').on('click', function() {
		choosecar("{:url('rent/index',['op'=>'search'])}")
	});
	$('.back').on('click', function() {
		window.history.back()
	});
	
	
})
